//
//  WelcomeController.swift
//  DiZhenYuJing
//
//  Created by zhibao on 2024/2/3.
//

import UIKit
class WelcomeController: UIViewController,SDCycleScrollViewDelegate {
    var contentArray = Array<Dictionary<String, String>>()
    var nextBtn:UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        contentArray.append(
            [
                "title":LocalizedString("地震灾害预警"),
                "detail":LocalizedString("实时监测地震灾害，让您及时了解动态")
            ]
        )
        contentArray.append(
            [
                "title":LocalizedString("秒级传达"),
                "detail":LocalizedString("收到地震波，及时传达至APP，多一分逃脱希望")
                
            ]
        )
        contentArray.append(
            [
                "title":LocalizedString("目标距离米级"),
                "detail":LocalizedString("精确计算您与地震源距离，精准到米")
            ]
        )
        
        let cycleScrollView = SDCycleScrollView(frame: CGRectMake(0, pixw(p: 100), SCREEN_WIDTH, pixw(p:168)), shouldInfiniteLoop: true, imageNamesGroup: ["s","s","s"])
        cycleScrollView?.backgroundColor = UIColor.clear
        cycleScrollView?.infiniteLoop = true
        cycleScrollView?.autoScroll = true
        cycleScrollView?.autoScrollTimeInterval = 4
        cycleScrollView?.bannerImageViewContentMode = .scaleAspectFit
        //cycleScrollView?.pageControlStyle = .pagec
        cycleScrollView?.pageDotColor = MYColor.color(withHexString: "666666", alpha: 0.3)
        cycleScrollView?.currentPageDotColor = MYColor.color(withHexString:"666666")
        cycleScrollView?.pageControlBottomOffset = -10
        self.view.addSubview(cycleScrollView!)
        cycleScrollView?.snp.makeConstraints({ make in
            make.left.right.centerY.equalTo(self.view)
            make.height.equalTo(pixw(p:168))
        })
        cycleScrollView?.delegate = self
        cycleScrollView?.scrollDirection = .horizontal
        
        let jumpBtn = UIButton(type: .custom)
        jumpBtn.layer.cornerRadius = pixw(p:19)
        jumpBtn.layer.borderWidth = 0.5
        jumpBtn.layer.borderColor = MYRed.cgColor
        jumpBtn.addTarget(self, action: #selector(gotoController), for: .touchUpInside)
        jumpBtn.setTitle("    \(LocalizedString("跳过"))    ", for: .normal)
        jumpBtn.setTitleColor(MYRed, for: .normal)
        jumpBtn.titleLabel?.font = MY_SCALE_RegularFONT(15)
        self.view.addSubview(jumpBtn)
        jumpBtn.snp.makeConstraints { make in
            make.height.equalTo(pixw(p:38))
            make.right.equalTo(pixw(p:-20))
            make.top.equalTo(statusBarHeight())
        }
        
        nextBtn = UIButton(type: .custom)
        nextBtn.layer.cornerRadius = pixw(p:19)
        nextBtn.isHidden = true
        nextBtn.backgroundColor = MYBlue
        nextBtn.addTarget(self, action: #selector(gotoController), for: .touchUpInside)
        nextBtn.setTitle(LocalizedString("去往APP"), for: .normal)
        nextBtn.setTitleColor(UIColor.white, for: .normal)
        nextBtn.titleLabel?.font = MY_SCALE_RegularFONT(15)
        self.view.addSubview(nextBtn)
        nextBtn.snp.makeConstraints { make in
            make.height.equalTo(pixw(p:46))
            make.right.equalTo(pixw(p:-50))
            make.left.equalTo(pixw(p:50))
            make.top.equalTo(cycleScrollView!.snp.bottom).offset(30)
        }
    }
    
    @objc func gotoController() {
        UserDefaults.standard.setValue(WelcomeFlag, forKey: WelcomeFlag)
        let controller = TabBarController()
        keyWindow().rootViewController = controller
    }
}

extension WelcomeController{
    func customCollectionViewCellClass(for view: SDCycleScrollView!) -> AnyClass! {
        return UICollectionViewCell.self
    }
    
    func setupCustomCell(_ cell: UICollectionViewCell!, for index: Int, cycleScrollView view: SDCycleScrollView!) {
        for subView in cell.contentView.subviews {
            if subView.tag == 1000 {
                subView.removeFromSuperview()
            }
        }
        
        let titleLabel = UILabel()
        titleLabel.text = contentArray[index]["title"]!
        titleLabel.font = MY_SCALE_SemiboldFONT(28)
        titleLabel.textColor = MYRed
        titleLabel.tag = 1000
        titleLabel.numberOfLines = 0
        titleLabel.lineBreakMode = .byWordWrapping
        titleLabel.textAlignment = .center
        cell.contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.left.equalTo(pixw(p:30))
            make.right.equalTo(pixw(p:-30))
        }
        
        let detailLabel = UILabel()
        detailLabel.text = contentArray[index]["detail"]!
        detailLabel.font = MY_SCALE_HeavyFONT(18)
        detailLabel.textColor = MYBlue
        detailLabel.tag = 1000
        detailLabel.numberOfLines = 0
        detailLabel.lineBreakMode = .byWordWrapping
        detailLabel.textAlignment = .center
        cell.contentView.addSubview(detailLabel)
        detailLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(25)
            make.right.left.equalTo(titleLabel)
        }
    }
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didScrollTo index: Int) {
        if index == contentArray.count - 1 {
            nextBtn.isHidden = false
        }else{
            nextBtn.isHidden = true
        }
    }
}
